package cn.zwy.tree.day1;

import java.io.PrintWriter;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.TraceClassVisitor;

public class Demo1 implements Opcodes{
    public static void main(String[] args) {
        
        // 这样写代码可是比之前的代码爽多了
        ClassNode cn = new ClassNode();
        cn.version = V1_5;
        cn.access = ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE;
        cn.name = "pkg/Comparable";
        cn.superName = "java/lang/Object";
        cn.interfaces.add("pkg/Mesurable");
        cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "LESS", "I", null, new Integer(-1)));
        cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "EQUAL", "I", null, new Integer(0)));
        cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "GREATER", "I", null, new Integer(1)));
        cn.methods.add(new MethodNode(ACC_PUBLIC + ACC_ABSTRACT,
        "compareTo", "(Ljava/lang/Object;)I", null, null));
        
        
        TraceClassVisitor classVisitor = new TraceClassVisitor(new PrintWriter(System.out));
        cn.accept(classVisitor);
    }
    
    /*ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_8, ACC_PUBLIC, "cn/zwy/asm/DeptBean", null, "java/lang/Object",
        null);
    cw.visitSource("Bean.java", null);
    FieldVisitor fv = cw.visitField(ACC_PRIVATE, "f", "I", null, null);
    if (fv != null) {
      fv.visitEnd();
    }
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    if (mv != null) {
      mv.visitCode();
      mv.visitVarInsn(ALOAD, 0);
      mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
          "()V", false);
      mv.visitInsn(RETURN);
      mv.visitMaxs(1, 1);
      mv.visitEnd();
    }*/
}
